package com.jixiantubu.luntan;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.example.jixiantubu.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jixiantubu.adapter.Reply_ReplyAdapter;
import com.jixiantubu.bean.ReplyInfoBean;
import com.jixiantubu.util.MyApplication;
import com.jixiantubu.util.NoScrollListView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Reply_replyActivity extends Activity implements OnClickListener {
	private NoScrollListView listView;
	private HttpUtils httpUtils = new HttpUtils();
	private ImageView imageView1, imageView2, imageView3, imageView4;
	private TextView textView1, textView2, textView3, textView4, textView5;
	private Intent intent;
	private Reply_ReplyAdapter adapter;
	private ReplyInfoBean replyInfoBean;
	private EditText editText;
	private BitmapUtils bitmapUtils;
	private Context context = Reply_replyActivity.this;
	private String content;
	private RequestParams params;
	List<ReplyInfoBean> list = new ArrayList<ReplyInfoBean>();
	List<ReplyInfoBean> list2 = new ArrayList<ReplyInfoBean>();
	String url1 = "http://" + MyApplication.getIP()
			+ ":8080/Jinnang/reply_servlet";
	LayoutInflater inflater;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_reply_reply);
		initViews();
		initData();
	}

	private void initData() {
		String username = replyInfoBean.getReplyname();
		adapter = new Reply_ReplyAdapter(Reply_replyActivity.this, list,
				username);
		listView.setAdapter(adapter);
		setListViewHeightBasedOnChildren(listView);
		params = new RequestParams();
		params.addQueryStringParameter("key", "4");
		params.addQueryStringParameter("uid",
				String.valueOf(replyInfoBean.getReplyuser_id()));
		params.addQueryStringParameter("topic",
				String.valueOf(replyInfoBean.getPi_TopicID()));
		params.addQueryStringParameter("rpid",
				String.valueOf(replyInfoBean.getRp_id()));
		httpUtils.send(HttpMethod.POST, url1, params,
				new RequestCallBack<String>() {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						String result = responseInfo.result;
						Gson gson = new Gson();
						Type typeOfT = new TypeToken<List<ReplyInfoBean>>() {
						}.getType();
						list = gson.fromJson(result, typeOfT);

						for (ReplyInfoBean replyInfo : list) {
							if (replyInfo.getRp_id() == replyInfoBean
									.getRp_id() && replyInfo.getRank() == 0) {
								list2.add(replyInfo);
							}
						}
						adapter.setList(list2);
						adapter.notifyDataSetChanged();
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {

					}
				});
	}

	private void initViews() {
		inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View headView = inflater.inflate(R.layout.reply_info_item, null);
		context = Reply_replyActivity.this;
		bitmapUtils = new BitmapUtils(context);
		intent = getIntent();
		// Log.e("我的账号", MyApplication.user_id);
		replyInfoBean = (ReplyInfoBean) intent
				.getSerializableExtra("reply_post");
		// Log.e(">>>>>>>>>>>>>>>>>>>>>", replyInfoBean.toString());

		imageView1 = (ImageView) findViewById(R.id.reply_user_imgview_info);
		imageView2 = (ImageView) headView.findViewById(R.id.huifu_reply);
		imageView3 = (ImageView) findViewById(R.id.reply_tiezi_info_fasong);
		imageView4 = (ImageView) findViewById(R.id.reply_fabiao_fanhui);
		textView1 = (TextView) findViewById(R.id.reply_username_textview_info);
		textView2 = (TextView) findViewById(R.id.reply_rank_textview_info);
		textView3 = (TextView) findViewById(R.id.reply_createTime_textview_info);
		textView4 = (TextView) findViewById(R.id.reply_content_textview_info);
		textView5 = (TextView) findViewById(R.id.tiezi_info_louci_Textview);

		editText = (EditText) findViewById(R.id.reply_huifuneirong);
		listView = (NoScrollListView) findViewById(R.id.reply_info_listview);
		// listView.addHeaderView(headView);
		textView1.setText(replyInfoBean.getReplyname());
		textView2.setText("第" + (replyInfoBean.getRank() + 1) + "楼");
		textView5.setText("第" + (replyInfoBean.getRank() + 1) + "楼");
		textView3.setText(replyInfoBean.getCreateTime());
		textView4.setText(replyInfoBean.getContent());
		String url = "http://" + MyApplication.getIP() + ":8080/Jinnang/img/"
				+ replyInfoBean.getReplyname_imgpath();
		bitmapUtils.display(imageView1, url);
		imageView2.setImageResource(R.drawable.huifu);
		imageView3.setImageResource(R.drawable.group_fold_arrow);
		imageView4.setImageResource(R.drawable.houtuijian);
		imageView2.setOnClickListener(this);
		imageView3.setOnClickListener(this);
		imageView4.setOnClickListener(this);

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.reply_reply_imgview:
			onFocusChange(true);

			break;
		case R.id.reply_tiezi_info_fasong:
			sendreply();
			break;
		case R.id.reply_fabiao_fanhui:
			finish();
			break;
		default:
			break;
		}
	}

	private void sendreply() {
		content = editText.getText().toString();
		if (isEditEmply()) {
			httpUtils = new HttpUtils();
			params = new RequestParams();
			params.addBodyParameter("key", "5");
			params.addBodyParameter("content", content);
			params.addBodyParameter("be_replyid", MyApplication.user_id);
			params.addBodyParameter("userid",
					String.valueOf(replyInfoBean.getReplyuser_id()));
			params.addBodyParameter("topic_id",
					String.valueOf(replyInfoBean.getPi_TopicID()));
			params.addQueryStringParameter("rpid",
					String.valueOf(replyInfoBean.getRp_id()));
			System.out.println("content" + content);
			System.out.println("be_replyid" + MyApplication.user_id);
			System.out.println("userid"
					+ String.valueOf(replyInfoBean.getReplyuser_id()));
			System.out.println("topic_id"
					+ String.valueOf(replyInfoBean.getPi_TopicID()));
			System.out.println("rpid"
					+ String.valueOf(replyInfoBean.getRp_id()));
			httpUtils.send(HttpMethod.POST, url1, params,
					new RequestCallBack<String>() {

						@Override
						public void onFailure(HttpException arg0, String arg1) {
						}

						@Override
						public void onSuccess(ResponseInfo<String> responseInfo) {
							listView.postDelayed(new Runnable() {
								@Override
								public void run() {
									onFocusChange(false);
									list2.clear();
									initData();
								}
							}, 1000);

						}
					});
		}
	}

	private void onFocusChange(boolean hasFocus) {
		final boolean isFocus = hasFocus;
		(new Handler()).postDelayed(new Runnable() {
			public void run() {
				InputMethodManager imm = (InputMethodManager) editText
						.getContext().getSystemService(INPUT_METHOD_SERVICE);
				if (isFocus) {
					// 显示输入法
					imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
				} else {
					// 隐藏输入法
					imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
				}
			}
		}, 100);
	}

	private Object comment;

	private boolean isEditEmply() {
		comment = editText.getText().toString().trim();
		if (comment.equals("")) {
			Toast.makeText(getApplicationContext(), "评论不能为空",
					Toast.LENGTH_SHORT).show();
			return false;
		}
		editText.setText("");
		return true;
	}

	private void setListViewHeightBasedOnChildren(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}
		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}
		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
		// Log.e(">>>>>>>>>>>>>>>", String.valueOf(params.height));
	}
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 10) {

				onFocusChange(false);
			}
		}
	};
}
